from django.db import models
from django.utils import timezone
import datetime;


# Create your models here.

class Reporter(models.Model):
    full_name = models.CharField(max_length=70)

    def __str__(self):  # __unicode__ on Python 2
        return self.full_name


class Article(models.Model):
    pub_date = models.DateField()
    headline = models.CharField(max_length=200)
    content = models.TextField()
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

    def __str__(self):  # __unicode__ on Python 2
        return self.headline


class Question(models.Model):
    question_text = models.CharField("问题描述", max_length=200)
    pub_date = models.DateTimeField("发布时间")

    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = '最新发布?'

    class Meta:
        verbose_name = '问题'
        verbose_name_plural = '问题'

    def __str__(self):
        return self.question_text


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    class Meta:
        verbose_name = '选项'
        verbose_name_plural = '选项'

    def __str__(self):
        return self.choice_text
